#ifndef __UACIN_H__
#define __UACIN_H__

#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <signal.h>
#include <stdio.h>
#include <string>
#include <map>
#include <set>
#include <functional>
#include <mutex>
#include "json.h"
#include "UsbDevice.h"

namespace Usbstream{

#ifdef __cplusplus
extern "C" {
#endif



using namespace std;

class UacIn : public  UsbDevice{
public:
	UacIn(int soundnum);
	~UacIn();



	bool getSupportParams(Json::Value &params);

	bool setParams(const Json::Value params);

	/**
	 * 做取流的一些前置工作，将声卡置于空闲的状态，声卡采集准备，并且计算好buffer的大小
	 * */
	bool startStream(callback_t cb);

	void processStream(callback_t cb);


	bool stopStream();



private:
	bool processBuffer(void *buffer, int length , callback_t cb);
	/**
	 * 初始化对象
	 * @param in:direction 要创建的对象是否用于音频输入
	 * */

	bool initUacIn();

	/**
	 * 反初始化设备对象
	 * */
	bool deInitUacIn();

private:
static mutex sm_UacInLock;

	int  m_soundnum;
	string m_usbPort;
	Json::Value m_supportParam;
	snd_pcm_t *m_captureHandle;
	snd_pcm_hw_params_t *m_hwParams;
	//一次采集的buffer
	void *m_buffer;
	unsigned int m_bufferSize;
	unsigned int m_channels;
	unsigned int m_rates;
	string m_format;
	//用于处理一个buffer的回调
	callback_t m_callback;
	//当前对象是否在使用
	bool m_used = false;
	//当前的是否在取流
	bool m_running = false;
	bool m_uacininit;
	bool m_exitOldThread;
	long long  m_index = 0;

};

#ifdef __cplusplus
}
#endif
}

#endif
